package com.example.controller;

import com.example.pojo.Users;
import com.example.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("users")
public class UsersController {

    @Autowired
    private UsersService usersService;

    @RequestMapping("{page}")
    public String showPage(@PathVariable String page) {
        return page;
    }

    @RequestMapping("addUser")
    public String addUser(Users users) {
        usersService.addUser(users);
        return "ok";
    }

    @RequestMapping("findUserAll")
    public String findUserAll(Model model) {
        List<Users> list = usersService.findUserAll();
        model.addAttribute("list", list);
        return "showUsers";
    }

    @RequestMapping("findUserById")
    public String findUserById(Integer id, Model model) {
        Users user = usersService.findUserById(id);
        model.addAttribute("user", user);
        return "updateUsers";
    }

    @RequestMapping("editUser")
    public String editUser(Users users) {
        usersService.updateUser(users);
        return "ok";
    }

    @RequestMapping("delUser")
    public String delUser(Integer id) {
        usersService.deleteUserById(id);
        return "redirect:/users/findUserAll";
    }

}
